package com.ruoyi.yqhs.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.yqhs.mapper.YqhsQybMapper;
import com.ruoyi.yqhs.domain.YqhsQyb;
import com.ruoyi.yqhs.service.IYqhsQybService;

/**
 * 气液比数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-31
 */
@Service
public class YqhsQybServiceImpl implements IYqhsQybService {
    @Autowired
    private YqhsQybMapper yqhsQybMapper;

    /**
     * 查询气液比数据
     *
     * @param id 气液比数据主键
     * @return 气液比数据
     */
    @Override
    public YqhsQyb selectYqhsQybById(Long id) {
        return yqhsQybMapper.selectYqhsQybById(id);
    }

    /**
     * 查询气液比数据列表
     *
     * @param yqhsQyb 气液比数据
     * @return 气液比数据
     */
    @Override
    public List<YqhsQyb> selectYqhsQybList(YqhsQyb yqhsQyb) {
        return yqhsQybMapper.selectYqhsQybList(yqhsQyb);
    }

    /**
     * 新增气液比数据
     *
     * @param yqhsQyb 气液比数据
     * @return 结果
     */
    @Override
    public int insertYqhsQyb(YqhsQyb yqhsQyb) {
        yqhsQyb.setId(IdUtils.getId());
        yqhsQyb.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        yqhsQyb.setCreateTime(DateUtils.getNowDate());
        return yqhsQybMapper.insertYqhsQyb(yqhsQyb);
    }

    /**
     * 修改气液比数据
     *
     * @param yqhsQyb 气液比数据
     * @return 结果
     */
    @Override
    public int updateYqhsQyb(YqhsQyb yqhsQyb) {
        yqhsQyb.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        yqhsQyb.setUpdateTime(DateUtils.getNowDate());
        return yqhsQybMapper.updateYqhsQyb(yqhsQyb);
    }

    /**
     * 批量删除气液比数据
     *
     * @param ids 需要删除的气液比数据主键
     * @return 结果
     */
    @Override
    public int deleteYqhsQybByIds(Long[] ids) {
        return yqhsQybMapper.deleteYqhsQybByIds(ids);
    }

    /**
     * 删除气液比数据信息
     *
     * @param id 气液比数据主键
     * @return 结果
     */
    @Override
    public int deleteYqhsQybById(Long id) {
        return yqhsQybMapper.deleteYqhsQybById(id);
    }

    /**
     * 获取实时数据
     *
     * @param jyzId
     */
    @Override
    public List<Map<String, Object>> getRtInfo(Long jyzId) {
        BigDecimal qyb = BigDecimal.ZERO;

        //先获取最新的气液比
        List<Map<String, Object>> list = yqhsQybMapper.selectLastQyb(jyzId);
        PageInfo pageInfo = PageInfo.of(list);

        Map<String, Map<String, Object>> qybMap = new HashMap<>();

        for (Map<String, Object> map : list) {
            qybMap.put(map.get("sb_id").toString(), map);
        }
        //加油次数
        long jycs = pageInfo.getTotal();
        //获取总燃油量
        List<Map<String, Object>> rylList = yqhsQybMapper.selectSumRyl(jyzId);
        rylList.forEach(a -> {
            a.put("qyb", qybMap.get(a.get("sb_id").toString()).get("qyb"));
            a.put("curtime", qybMap.get(a.get("sb_id").toString()).get("curtime"));

        });
        return rylList;
    }

}
